<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.danceforyou.${objectName?uncap_first}.dao.${objectName}DAO">

    <!--字段-->
    <sql id="baseColumnList">
        ${tableName}.id
    <#list columnList as column>
        ,${tableName}.${column.columnName}
    </#list>
    </sql>

    <!--DO结果集-->
    <resultMap id="doMap" type="com.danceforyou.${objectName?uncap_first}.model.${objectName}DO">
        <id column="id" property="id" />
    <#list columnList as column>
        <result column="${column.columnName}" property="${column.humpColumnName}"/>
    </#list>
    </resultMap>

    <!--VO结果集-->
    <resultMap id="voMap" extends="doMap" type="com.danceforyou.${objectName?uncap_first}.vo.${objectName}VO">

    </resultMap>

    <!--通用查询条件-->
    <sql id="baseConditionSql">
        <if test="condition != null">
        <#list columnList as column>
            <!--${column.columnComment}-->
            <if test="condition.${column.humpColumnName} != null">
                AND ${tableName}.${column.columnName} = ${r"#{condition."}${column.humpColumnName}${r"}"}
            </if>
        </#list>
        </if>
    </sql>

    <!--新增数据-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO ${tableName}
        <trim prefix="(" suffixOverrides="," suffix=")" >
        <#list columnList as column>
            <if test="${column.humpColumnName} != null">${column.columnName},</if>
        </#list>
        </trim>
        <trim prefix="VALUES (" suffixOverrides="," suffix=")" >
        <#list columnList as column>
            <if test="${column.humpColumnName} != null">${r"#{"}${column.humpColumnName}${r"}"},</if>
        </#list>
        </trim>
    </insert>

    <!--更新数据（不更新为null的列）-->
    <update id="update">
        UPDATE ${tableName}
        <trim prefix="SET" suffixOverrides=",">
        <#list columnList as column>
            <if test="${column.humpColumnName} != null">${column.columnName} = ${r"#{"}${column.humpColumnName}${r"}"},</if>
        </#list>
        </trim>
        WHERE id = ${r"#{id}"}
    </update>

    <!--全列更新-->
    <update id="updateWhole">
        UPDATE ${tableName}
        <trim prefix="SET" suffixOverrides=",">
        <#list columnList as column>
            <if test="${column.humpColumnName} != null">${column.columnName} = ${r"#{"}${column.humpColumnName}${r"}"},</if>
        </#list>
        </trim>
        WHERE id = ${r"#{id}"}
    </update>

    <!--根据指定字段更新数据（不更新为null的列）-->
    <update id="updateByProperty">
        UPDATE ${tableName}
        <trim prefix="SET" suffixOverrides=",">
        <#list columnList as column>
            <if test="${column.humpColumnName} != null">${column.columnName} = ${r"#{"}${column.humpColumnName}${r"}"},</if>
        </#list>
        </trim>
        WHERE ${r"${property}"} = ${r"#{value}"}
    </update>

    <!--根据指定字段全列更新-->
    <update id="updateWholeByProperty">
        UPDATE ${tableName}
        <trim prefix="SET" suffixOverrides=",">
        <#list columnList as column>
            <if test="${column.humpColumnName} != null">${column.columnName} = ${r"#{"}${column.humpColumnName}${r"}"},</if>
        </#list>
        </trim>
        WHERE ${r"${property}"} = ${r"#{value}"}
    </update>

    <!--根据主键单条删除-->
    <delete id="deleteById">
        DELETE FROM ${tableName} WHERE id = ${r"#{id}"}
    </delete>

    <!--根据主键批量删除-->
    <delete id="deleteByIds">
        DELETE FROM ${tableName}
        <where>
            id IN
            <foreach item="id" index="index" collection="idList" open="(" separator="," close=")">
                ${r"#{id}"}
            </foreach>
        </where>
    </delete>

    <!--根据指定字段删除-->
    <delete id="deleteByProperty">
        DELETE FROM ${tableName} WHERE ${r"${property}"} = ${r"#{value}"}
    </delete>

    <!--根据条件批量删除-->
    <delete id="deleteByCondition">
        DELETE FROM ${tableName}
        <where>
            <include refid="baseConditionSql"/>
        </where>
    </delete>

    <!--根据主键单条标记为删除-->
    <update id="setDeleteById">
        UPDATE ${tableName} SET isDelete = '1' WHERE id = ${r"#{id}"}
    </update>

    <!--根据主键批量标记为删除-->
    <update id="setDeleteByIds">
        UPDATE ${tableName} SET isDelete = '1'
        <where>
            id IN
            <foreach item="id" index="index" collection="idList" open="(" separator="," close=")">
                ${r"#{id}"}
            </foreach>
        </where>
    </update>

    <!--根据指定字段标记为删除-->
    <update id="setDeleteByProperty">
        UPDATE ${tableName} SET isDelete = '1' WHERE ${r"${property}"} = ${r"#{value}"}
    </update>

    <!--根据条件批量标记为删除-->
    <update id="setDeleteByCondition">
        UPDATE ${tableName} SET isDelete = '1'
        <where>
            <include refid="baseConditionSql"/>
        </where>
    </update>

    <!--根据主键获取数据-->
    <select id="getById" resultMap="doMap">
        SELECT <include refid="baseColumnList"/>
        FROM ${tableName}
        WHERE id = ${r"#{id}"}
    </select>

    <!--根据指定字段获取数据-->
    <select id="getByProperty" resultMap="doMap">
        SELECT <include refid="baseColumnList"/>
        FROM ${tableName}
        WHERE ${r"${property}"} = ${r"#{value}"}
        <if test="orderBy != null">
            ORDER BY ${r"${orderBy}"} ${r"${sortBy}"}
        </if>
        LIMIT 1
    </select>

    <!--根据条件获取数据-->
    <select id="getByCondition" resultMap="doMap">
        SELECT <include refid="baseColumnList"/>
        FROM ${tableName}
        <where>
            <include refid="baseConditionSql"/>
        </where>
        <if test="orderBy != null">
            ORDER BY ${r"${orderBy}"} ${r"${sortBy}"}
        </if>
        LIMIT 1
    </select>

    <!--获取数据列表-->
    <select id="listAll" resultMap="doMap">
        SELECT <include refid="baseColumnList"/>
        FROM ${tableName}
        <if test="orderBy != null">
            ORDER BY ${r"${orderBy}"} ${r"${sortBy}"}
        </if>
    </select>

    <!--根据指定字段获取数据列表-->
    <select id="listByProperty" resultMap="doMap">
        SELECT <include refid="baseColumnList"/>
        FROM ${tableName}
        WHERE ${r"${property}"} = ${r"#{value}"}
        <if test="orderBy != null">
            ORDER BY ${r"${orderBy}"} ${r"${sortBy}"}
        </if>
    </select>

    <!--根据条件获取数据列表-->
    <select id="listByCondition" resultMap="doMap">
        SELECT <include refid="baseColumnList"/>
        FROM ${tableName}
        <where>
            <include refid="baseConditionSql"/>
        </where>
        <if test="orderBy != null">
            ORDER BY ${r"${orderBy}"} ${r"${sortBy}"}
        </if>
    </select>

    <!--查询数据列表-->
    <select id="queryList" resultMap="voMap">
        SELECT <include refid="baseColumnList"/>
        FROM ${tableName}
        <where>
            <include refid="baseConditionSql"/>
        </where>
        <if test="orderBy != null">
            ORDER BY ${r"${orderBy}"} ${r"${sortBy}"}
        </if>
    </select>

    <!--分页查询数据列表-->
    <select id="queryPage" resultMap="voMap">
        SELECT <include refid="baseColumnList"/>
        FROM ${tableName}
        <where>
            <include refid="baseConditionSql"/>
        </where>
        <if test="orderBy != null">
            ORDER BY ${r"${orderBy}"} ${r"${sortBy}"}
        </if>
        LIMIT ${r"${offset}"}, ${r"${rows}"}
    </select>

    <!--根据条件查询总条数-->
    <select id="countByCondition" resultType="java.lang.Integer">
        SELECT IFNULL(COUNT(1), 0)
        FROM ${tableName}
        <where>
            <include refid="baseConditionSql"/>
        </where>
    </select>

    <!--分页查询统计项-->
    <select id="statsByCondition" resultType="java.util.Map">
        SELECT IFNULL(COUNT(1), 0)
        FROM ${tableName}
        <where>
            <include refid="baseConditionSql"/>
        </where>
    </select>

    <!--根据条件求和-->
    <select id="sumByCondition" resultType="java.math.BigDecimal">
        SELECT IFNULL(SUM(${r"${property}"}), 0)
        FROM ${tableName}
        <where>
            <include refid="baseConditionSql"/>
        </where>
    </select>

</mapper>